package com.guowen.myaccount;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.guowen.myaccount.frag_history.MonthFragment;
import com.guowen.myaccount.frag_history.YearFragment;

public class HistoryActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageView backIv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);

        backIv = findViewById(R.id.activity_history_back);
        backIv.setOnClickListener(this);

        ViewPager2 viewPagerFragments=findViewById(R.id.activity_history_vp2_content);
        viewPagerFragments.setAdapter(new MyFragmentAdpater(this));

        TabLayout tabLayoutHeader= findViewById(R.id.activity_history_tab_header);
        TabLayoutMediator tabLayoutMediator=new TabLayoutMediator(tabLayoutHeader, viewPagerFragments, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                switch (position) {
                    case 0:
                        tab.setText("按月");
                        break;
                    case 1:
                        tab.setText("按年");
                        break;
                }
            }
        });
        tabLayoutMediator.attach();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_history_back:
                finish();
                break;
        }
    }

    private class MyFragmentAdpater extends FragmentStateAdapter {


        public MyFragmentAdpater(@NonNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            switch(position)
            {
                case 0:
                    return MonthFragment.newInstance();
                default:
                    return YearFragment.newInstance();
            }
        }

        @Override
        public int getItemCount() {
            return 2;
        }
    }
}